<?php

class AlbumController extends Zend_Controller_Action{
    function indexAction(){
        echo "Controller Album";
        $album = new Model_Album();
        $this->view->data = $album->listAll();
    }
    function addAction(){
        $form = new Form_Album();
        $this->view->form = $form;
        if($this->getRequest()->isPost()){
            $formData = $this->getRequest()->getParams();
            if($form->isValid($formData)){
                $artist = $form->getValue('artist');
                $title = $form->getValue('title');
                $album = new Model_Album();
                if($album->addAlbum($title, $artist)== true){
                    echo "Them du lieu thanh cong";
                    $this->_helper->redirector('index');
                }
                else {
                    echo "Da co loi khi them du lieu";
                }
            }
        }
    } 
    public function editAction()
    {
        $form = new Form_Album();
        $form->submit->setLabel('Save');
        $this->view->form = $form;
        
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int)$form->getValue('id');
                $artist = $form->getValue('artist');
                $title = $form->getValue('title');
                $albums = new Model_Album();
                $albums->updateAlbum($id, $artist, $title);
                
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $albums = new Model_Album();
                $form->populate($albums->getAlbum($id));
            }
        }
        
    }
    function deleteAction(){
        $id = $this->_getParam('id', 0);
        $this->view->id = $id;
//        if($this->getRequest()->isPost()){
//            $id = $this->_getParam('id');
//            $this->view->id = $id;
//        }
    }
    

    public function formAction1(){
        $form = new Form_User();
        $this->view->form = $form;
    }
}
?>
